package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Game;
import com.kkb.pojo.Team;
import com.kkb.service.GameService;
import com.kkb.service.TeamService;
import com.kkb.vo.GameQueryVO;
import com.kkb.vo.QueryVO;
import com.kkb.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("game")
@ResponseBody
public class GameController {
    @Resource
    private GameService gameService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Game> queryByPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, GameQueryVO vo) {
        PageInfo<Game> info = gameService.queryByPage(pageNum, pageSize, vo);
        System.out.println(pageNum);
        return new ResultVO<>(info);
    }

    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Game> addGame(Game game) {
        int i = gameService.addGame(game);
        if (i == -1) {
            return new ResultVO<Game>(201, "主队ID有误，请检查！");
        }
        if (i == -2) {
            return new ResultVO<Game>(201, "客队ID有误，请检查！");
        }
        if (i == 1) {
            return new ResultVO<Game>();
        }
        return new ResultVO<Game>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Game> queryById(@PathVariable("id") int gameId) {
        Game game = gameService.queryById(gameId);
        return new ResultVO<>(game);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public ResultVO<Game> updateGame(@PathVariable("id") int gameId, Game game) {
        game.setGameId(gameId);
        int i = gameService.updateGame(game);
        if (i == -1) {
            return new ResultVO<Game>(201, "主队ID有误，请检查！");
        }
        if (i == -2) {
            return new ResultVO<Game>(201, "客队ID有误，请检查！");
        }
        if (i == 1) {
            return new ResultVO<Game>();
        }
        return new ResultVO<Game>(500, "服务器内部异常，请稍后再试！");
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Game> deleteTeam(@PathVariable("id") int gameId) {
        int i = gameService.deleteGame(gameId);
        if (i == 1) {
            return new ResultVO<Game>();
        }
        return new ResultVO<Game>(500, "服务器内部异常，请稍后再试！");
    }
}
